//
//  HKListSectionModel.m
//  KSBridgeDesign
//
//  Created by huzhaoke on 2021/4/17.
//

#import "HKListSectionModel.h"
#import "HKListSectionModel+Private.h"

@interface HKListSectionModel ()

@property (nonatomic, readwrite) NSMutableArray<HKListCellModel *> *cellModels;

@end

@implementation HKListSectionModel

+ (instancetype)sectionModelWithCellModels:(HKListCellModel *)cellModels {
    HKListSectionModel *sectionModel = [self new];
    return sectionModel;
}

- (BOOL)isEmpty {
    return self.cellModels.count == 0;
}
- (NSInteger)cellModelCount {
    return self.cellModels.count;
}

- (NSInteger)totalCellHeight {
    return 0;
}

- (nullable __kindof HKListCellModel *)cellModelAtIndex:(NSUInteger)index {
    if (index >= self.cellModelCount) {
        return nil;
    }
    return self.cellModels[index];
}

- (void)clear {
    [self.cellModels removeAllObjects];
}

- (NSUInteger)indexOfCellModel:(HKListCellModel *)cellModel {
    if (!cellModel) {
        return NSNotFound;
    }
    return [self.cellModels indexOfObject:cellModel];
}

- (void)addCellModel:(HKListCellModel *)cellModel {
    if (!cellModel) {
        return;
    }
    [self.cellModels addObject:cellModel];
}

- (void)addCellModels:(NSArray<HKListCellModel *> *)cellModels {
    if (cellModels.count == 0) {
        return;
    }
    [self.cellModels addObjectsFromArray:cellModels];
}
- (void)insertCellModel:(HKListCellModel *)cellModel atIndex:(NSInteger)index {
    if (!cellModel || index > self.cellModelCount) {
        return;
    }
    [self.cellModels insertObject:cellModel atIndex:index];
}

- (void)insertCellModels:(NSArray<HKListCellModel *> *)cellModels atIndex:(NSInteger)index {
    if (cellModels.count == 0 || index > self.cellModelCount) {
        return;
    }
    
    [self.cellModels insertObjects:cellModels atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(index, cellModels.count)]];
}

- (void)removeCellModel:(HKListCellModel *)cellModel {
    if (!cellModel) {
        return;
    }
    [self.cellModels removeObject:cellModel];
    
}

- (void)removeCellModels:(NSArray<HKListCellModel *> *)cellModels {
    if (cellModels.count == 0) {
        return;
    }
    [self.cellModels removeObjectsInArray:cellModels];
}

- (void)removeCellModelAtIndex:(NSInteger)index {
    if (index >= self.cellModelCount) {
        return;
    }
    [self.cellModels removeObjectAtIndex:index];
}

- (BOOL)containsCellModel:(HKListCellModel *)cellModel {
    return [self.cellModels containsObject:cellModel];
}

//  Reload
- (void)reload {
    [self reloadWithAnimation:UITableViewRowAnimationNone];
}

- (void)reloadWithAnimation:(UITableViewRowAnimation)animation {
    if (self.reloadBlock) {
        self.reloadBlock(self, self.sectionIndexPath, animation, nil);
    }
}

- (NSIndexPath *)sectionIndexPath {
    return self.indexPath;
}


#pragma mark - properties
- (NSMutableArray<HKListCellModel *> *)cellModels {
    if (!_cellModels) {
        _cellModels = [NSMutableArray array];
    }
    return _cellModels;
}
@end
